/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.utility;

import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.base.aircraft.Aircraft;
import com.hitechbangla.fctu.base.aircraft.AircraftState;
import com.hitechbangla.fctu.base.aircraft.AircraftType;
import com.hitechbangla.fctu.base.base.BaseType;
import com.hitechbangla.fctu.base.base.Gbad;
import com.hitechbangla.fctu.base.base.GbadType;
import com.hitechbangla.fctu.base.map.FCTUMap;
import com.hitechbangla.fctu.base.radar.Radar;
import com.hitechbangla.fctu.base.radar.RadarType;
import com.hitechbangla.fctu.base.base.Base;
import com.hitechbangla.fctu.base.base.Kpi;
import com.hitechbangla.fctu.base.weather.Wind;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

//import java.util.List;
/**
 *
 * @author ASUS
 */
public class CustomSerializer {
    
    public static String objectToString(GameScene gameScene) {
        
        StringBuilder jSonBuilder = new StringBuilder();
        try {
        
      
        
        return toString(gameScene);
        
        //String string = toString( gameScene );
        ////System.out.println(" Encoded serialized version " );
        ////System.out.println( string );
        
        
        //GameScene some = ( GameScene ) fromString( string );
        ////System.out.println( "\n\nReconstituted object");
        ////System.out.println( some );
     
        
        
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
        //if(gameScene.
        
        //return jSonBuilder.toString();
    }
    
    public static GameScene stringToObject(String string){
        try {
            return ( GameScene ) fromString( string );
        }
        catch(Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    private static String toString( Serializable o ) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream( baos );
        oos.writeObject( o );
        oos.close();
        return new String( Base64Coder.encode( baos.toByteArray() ) );
    }
    
     /** Write the object to a Base64 string. */
    private static Object fromString( String s ) throws IOException ,
                                                        ClassNotFoundException {
        byte [] data = Base64Coder.decode( s );
        ObjectInputStream ois = new ObjectInputStream( 
                                        new ByteArrayInputStream(  data ) );
        Object o  = ois.readObject();
        ois.close();
        return o;
    }
}
